package com.qf.jdbc_01;

import java.sql.*;

/**
 * @Description: 公用常量
 * @Data: 2022/8/22 18:01
 * @Pacakge: com.qf.jdbc_01
 * @ClassName: JdbcSelect
 * @Author: fan
 * Jdbc查询
 * 1>导包
 * 2>注册驱动
 * 3>获取数据库连接对象
 * 4>准备sql语句
 * 5>通过数据库连接对象获取执行对象
 * 6>执行sql语句,将sql语句发送至mysql数据库中
 * 7>释放资源
 */
public class JdbcSelect {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1>导包
        //2>注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //3>获取数据库连接对象
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/racemall",
                "root",
                "f971014"
        );
        //4>准备sql语句
        String sql = "select * from admin";
        //5>通过数据库连接对象获取数据库执行对象
        Statement statement = connection.createStatement();
        //6>通过执行对象执行sql语句,发送给MySql数据库
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("用户编号\t\t用户名\t密码");
        //遍历结果集
        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            System.out.println(id+"\t\t\t"+username+"\t\t"+password);
        }
        //7>释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
